/**
 * 
 */
package cn.ad.server.web.spring.service.actions;

import cn.ad.server.web.log.IAdServerLogger;
import cn.ad.server.web.spring.model.AdRequest;
import cn.ad.server.web.spring.model.AdResponse;
import cn.ad.server.web.spring.service.actions.command.AdCallBackRequest;
import cn.ad.server.web.spring.service.actions.handler.gdt.server.HttpConnectionManager;
import cn.ad.server.biz.internal.AdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 激活回调-转发处理服务。
 * 
 * @author wangwei
 */
@Component("action-callback")
@Deprecated
public class AdCallBackRequestAction implements IAdRequestAction {

	private static final Logger LOGGER = LoggerFactory.getLogger(AdCallBackRequestAction.class);

	@Autowired
	private IAdServerLogger loggerService;

	@Autowired
	private AdService adService;

	private boolean debugMode;

	/**
	 * 
	 */
	@Override
	public AdResponse doRequest(AdRequest request) throws RequestActionException {
		try {
			if (request instanceof AdCallBackRequest) {
				AdCallBackRequest req = (AdCallBackRequest) request;
				return process(req);

			} else {
				throw new ClassCastException();
			}
		} finally {
			adService.unbond();
		}
	}

	/**
	 * 
	 * @param adRequest
	 */
	private AdResponse process(AdCallBackRequest adRequest) {
		// 通过新建连接。
		LOGGER.debug("AdTrackServlet doService 第三方渠道数据");
		String channelId = adRequest.getChannelID();
		String thirdPartyAddress = channelUrl.get("ch/" + channelId);
		LOGGER.debug("第三方渠道发送的地址参数：" + thirdPartyAddress + "?" + getDeviceInfo(adRequest));
		AdResponse resp = sendService(adRequest, thirdPartyAddress);
		return resp;
	}

	/**
	 * 发送服务。
	 * 
	 * @param trackCommand
	 * @return
	 */
	AdResponse sendService(AdCallBackRequest trackCommand, String thirdPartyAddress) {
		AdResponse resp = new AdResponse(200) {
		};
//<<<<<<< HEAD
		
//		if (StringUtils.isEmpty(thirdPartyAddress)) {
//			LOGGER.error("第三方渠道地址不存在。");
//			return resp;
//		}
//		
//		CloseableHttpClient httpClient = httpConnectionManager.getHttpClient();
//		HttpGet httpGet = new HttpGet(thirdPartyAddress + "?" + getDeviceInfo(trackCommand));
//		LOGGER.debug("第三方渠道发送的地址参数：" + thirdPartyAddress + "?" + getDeviceInfo(trackCommand));
//		try {
			//CloseableHttpResponse response = httpClient.execute(httpGet);
//			if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
//				LOGGER.debug("第三方渠道" + thirdPartyAddress + "发送成功");
//				resp.setStatus(HttpStatus.SC_OK);
//			} else {
//				LOGGER.error("失败状态：" + response.getStatusLine().getStatusCode());// TODO
//																					// do...
//				resp.setStatus(response.getStatusLine().getStatusCode());
//			}
//
//		} catch (Exception e) {
//			LOGGER.error("信息发送失败", e);// TODO do...
//			resp.setStatus(500);
//		}
//=======

		// if (StringUtils.isEmpty(thirdPartyAddress)) {
		// LOGGER.error("第三方渠道地址不存在。");
		// return resp;
		// }
		//
		// CloseableHttpClient httpClient =
		// httpConnectionManager.getHttpClient();
		// HttpGet httpGet = new HttpGet(thirdPartyAddress + "?" +
		// getDeviceInfo(trackCommand));
		// LOGGER.debug("第三方渠道发送的地址参数：" + thirdPartyAddress + "?" +
		// getDeviceInfo(trackCommand));
		// try {
		// CloseableHttpResponse response = httpClient.execute(httpGet);
		// if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
		// LOGGER.debug("第三方渠道" + thirdPartyAddress + "发送成功");
		// resp.setStatus(HttpStatus.SC_OK);
		// } else {
		// LOGGER.error("失败状态：" + response.getStatusLine().getStatusCode());//
		// TODO
		// // do...
		// resp.setStatus(response.getStatusLine().getStatusCode());
		// }
		//
		// } catch (Exception e) {
		// LOGGER.error("信息发送失败", e);// TODO do...
		// resp.setStatus(500);
		// }
//>>>>>>> 27d09310a5dccf231e32016c999e39807ebde5ee
		return resp;
	}

	/**
	 * 获取设备信息。
	 * 
	 * @param trackCommand
	 * @return
	 */
	private String getDeviceInfo(AdCallBackRequest trackCommand) {
		return trackCommand.toUrlDeviceInfo();
	}

	/**
	 * 第三方地址
	 */
	@Resource(name = "channelUrl")
	private Map<String, String> channelUrl;

	/**
	 * 连接管理器
	 */
	@Autowired
	private HttpConnectionManager httpConnectionManager;

}
